package com.suixun.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.codehaus.jackson.map.ObjectMapper;
import org.jsoup.helper.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.authority.controller.BaseManager;
import com.google.gson.Gson;
import com.suixun.entity.Factory;
import com.suixun.entity.form.OnlinepayForm;
import com.suixun.entity.page.PageModel;
import com.suixun.exception.OpServiceException;
import com.suixun.service.OnlinepayService;
import com.suixun.util.Constant;
import com.suixun.vo.ChongqingOrderVO;
import com.suixun.vo.DeliveryAddrVO;
import com.suixun.vo.DiscountOrderVO;
import com.suixun.vo.OnlinepayVO;
import com.suixun.vo.RenewalOrderVO;
/**
 * 
 * <p>Title: OnlinepayController</p>
 * <p> 功能描述:线上购买服务层 </p> 
 * @author	陈显强
 * @date	2016年12月19日下午3:10:05
 * @version 1.0
 */
@Controller
public class OnlinepayController extends BaseManager {
	protected static Logger logger = LoggerFactory.getLogger(OnlinepayController.class);
	@Resource
	private OnlinepayService onlinepayService;
	
	/**
	 * 
	 * <p>Title: queryOnlinepayList</p>
	 * <p> 功能描述:查询线上购买信息 </p>
	 * @param param
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/onlinepayManage/queryOnlinepayList.json")
	public Object queryOnlinepayList(String param){
		boolean bln=checkRight();
		if(!bln) {
			return fail(Constant.Authority.CHECK_FALSE);
		}
		try {
			OnlinepayForm onlinepay=new Gson().fromJson(param,OnlinepayForm.class);
			return success(onlinepayService.queryList(onlinepay));
		} catch (Exception e) {
			e.printStackTrace();
			logger("OnlinepayController queryOnlinepayList:"+e.getMessage(),e);
			return fail(Constant.EXCEPTION.OPERATE_FAIL);
		}
	}
	
	/**
	 * 
	 * <p>Title: validateIMEI</p>
	 * <p> 功能描述:验证IMEI号是否存在，以及和对应的厂商是否匹配,不做鉴权 </p>
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/validateIMEI.json")
	public Object validateIMEI(String param) {
		boolean bln=checkRight();
		if(!bln) {
			return fail(Constant.Authority.CHECK_FALSE);
		}
		if(param==null) {
			return fail(Constant.EMPTY);
		}
		try{
			OnlinepayForm onlinepay=new Gson().fromJson(param,OnlinepayForm.class);
			String isExist=onlinepayService.validateIMEI(onlinepay);
			HashMap<String ,String> result=new HashMap<String ,String>();
			if(Constant.TRUE.equals(isExist)) {
				result.put("resultinfo", "输入正确");
				return success(result);
			}else {
				return fail(isExist);
			}
		}catch (Exception e) {
			logger("OnlinepayController validateIMEI:"+e.getMessage(),e);
			return fail(Constant.EXCEPTION.OPERATE_FAIL);
		}
	}
	
	/**
	 * 
	 * <p>Title: sendDevice</p>
	 * <p> 功能描述:发货 </p>
	 * @param param
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/onlinepayManage/sendDevice.json")
	public Object sendDevice(String param) {
		boolean bln=checkRight();
		if(!bln) {
			return fail(Constant.Authority.CHECK_FALSE);
		}
		if(param==null) {
			return fail(Constant.EMPTY);
		}
		try {
			OnlinepayForm onlinepay=new Gson().fromJson(param,OnlinepayForm.class);
			if(StringUtils.isEmpty(onlinepay.getMdtid()) || StringUtils.isEmpty(onlinepay.getDeliveryno()) || 
					StringUtils.isEmpty(onlinepay.getDeliverycompany()) || StringUtils.isEmpty(onlinepay.getOrderno())) {
				return fail(Constant.EMPTY);
			}
			onlinepay.setOperationerid(getUserId().toString());
			int isAdd=onlinepayService.sendDevice(onlinepay);
			HashMap<String ,String> result=new HashMap<String ,String>();
			if(isAdd == 1) {
				result.put("resultinfo", Constant.SUCCESS);
				return success(result);
			}else {
				if (isAdd == -2) {
					return fail("该订单已发货，不能重复发货");
				}
				return fail(Constant.FAIL);
			}
		}catch (Exception e) {
			logger("OnlinepayController sendDevice:"+e.getMessage(),e);
			return fail(Constant.EXCEPTION.OPERATE_FAIL);
		}
	}
	
	@RequestMapping(value = "/onlinepayManage/sendChongqingDevice.json")
	@ResponseBody
	public Object sendChongqingDevice(String orderno, String sendTime, String mdtid){
		
		Long userid = getUserId();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date sendDate = null;
		try {
			sendDate = sdf.parse(sendTime);
		} catch (ParseException e1) {
			e1.printStackTrace();
		}
		if(StringUtil.isBlank(orderno) || sendDate == null || StringUtil.isBlank(mdtid)){
			return fail(Constant.PARAM_ERROR);
		}
		try{
			onlinepayService.sendChongqingDevice(orderno, sendDate, mdtid, Integer.valueOf(userid.toString()));
		}catch(Exception e){
			logger.error(e.getMessage(), e);
			return fail(e.getMessage());
		}
		
		return success();
	}
	
	@RequestMapping(value = "/onlinepayManage/modifyChongqingDevice.json")
	@ResponseBody
	public Object modifyChongqingDevice(String orderno, String sendTime, String mdtid){
		
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date sendDate = null;
		try {
			if(!StringUtil.isBlank(sendTime)){
				sendDate = sdf.parse(sendTime);
			}
		} catch (ParseException e1) {
			e1.printStackTrace();
		}
		if(StringUtil.isBlank(orderno)){
			return fail(Constant.PARAM_ERROR);
		}
		try{
			onlinepayService.modifyChongqingDevice(orderno, sendDate, mdtid);
		}catch(Exception e){
			logger.error(e.getMessage(), e);
			return fail(e.getMessage());
		}
		
		return success();
	}
	
	/**
	 * 
	 * <p>Title: exportOnlinepayList</p>
	 * <p> 功能描述:导出excel</p>
	 * @param param
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/onlinepayManage/export_OnlinepayList.json")
	public Object exportOnlinepayList(String param) {
		boolean bln = checkRight();
		if (!bln) {
			toRedirect(404);
		}
		try{
			OnlinepayForm onlinepay=new Gson().fromJson(param,OnlinepayForm.class);
			ArrayList<String> listParm = new ArrayList<String>();
			listParm.add(Constant.Excel.PAY_STATUS);
			listParm.add(Constant.Excel.SMARTCARD_NUM);
			listParm.add(Constant.Excel.WHITE_CARD_NUM1);
			listParm.add(Constant.Excel.WHITE_CARD_NUM2);
			listParm.add(Constant.Excel.PAY_MONEY);
			listParm.add(Constant.Excel.PAY_TIME);
			listParm.add(Constant.Excel.PEOPLE_NAME);
			listParm.add(Constant.Excel.PEOPLE_TEL);
			listParm.add(Constant.Excel.DELIVERY_ADDRESS);
			listParm.add(Constant.Excel.IMEI);
			listParm.add(Constant.Excel.DELIVERY_NUMBER);
			listParm.add(Constant.Excel.SEND_TIME);
			listParm.add(Constant.Excel.STATUS);
			List<List<String>> listValue=new LinkedList<List<String>>();
			//限制最多导出1万条数据，前端可以通过细化查询条件来多次导出
			if(onlinepay==null) {
				onlinepay=new OnlinepayForm();
			}
			if(StringUtil.isBlank(onlinepay.getPagecount()))
				onlinepay.setPagecount(Constant.Excel.EXPORT_ROWS);
			List<OnlinepayVO> pageList=onlinepayService.queryList(onlinepay).getQueryresult();
			//限制最多导出1万条数据，前端可以通过细化查询条件来多次导出
			for(OnlinepayVO onlinepayInfo:pageList) {
				List<String> value=new LinkedList<String>();
				value.add(onlinepayInfo.getPaystatus());
				value.add(onlinepayInfo.getSmartcardnum());
				value.add(onlinepayInfo.getWhitecard1num());
				value.add(onlinepayInfo.getWhitecard2num());
				value.add(onlinepayInfo.getPaymoney());
				value.add(onlinepayInfo.getPaytime());
				String deliveryaddr = null;
				if(StringUtils.isEmpty(onlinepayInfo.getDeliveryaddr())) {
					deliveryaddr = null;
				}else {
					ObjectMapper mapper = new ObjectMapper();
					DeliveryAddrVO address = null;
					address = mapper.readValue(onlinepayInfo.getDeliveryaddr(), DeliveryAddrVO.class);
					if(address != null && StringUtils.isNotEmpty(address.getName())) {
						onlinepayInfo.setPeoplename(address.getName());
					}
					deliveryaddr = new StringBuffer().append(address.getP()).append(address.getC())
							.append(address.getT()).append(address.getD()).toString();
				}
				value.add(onlinepayInfo.getPeoplename());
				value.add(onlinepayInfo.getPeopletel());
				value.add(deliveryaddr);
				value.add(onlinepayInfo.getMdtid());
				value.add(onlinepayInfo.getDeliveryno());
				value.add(onlinepayInfo.getSendtime());
				if (onlinepayInfo.getStatus() != null && onlinepayInfo.getStatus() == 0) {
					value.add("已取消");
				}else if (onlinepayInfo.getStatus() != null && onlinepayInfo.getStatus() == -1) {
					value.add("退款中");
				}else if (onlinepayInfo.getStatus() != null && onlinepayInfo.getStatus() == -2) {
					value.add("退款失败");
				}else if (onlinepayInfo.getStatus() != null && onlinepayInfo.getStatus() == 2) {
					value.add("退款成功");
				}else {
					if (onlinepayInfo.getDeliveryno() == null) {
						value.add("未发货");
					}else {
						value.add("已发货");
					}
				}
				listValue.add(value);
			}
			// 创建新的Excel 工作簿
			HSSFWorkbook workbook = exportDeviceList(Constant.Excel.ONLINEPAY_EXCEL,listParm,listValue);
			HttpServletResponse response=this.getResponse();
			response.setHeader("Content-disposition", "attachment;filename=onlinepayList.xls");
			workbook.write(this.getResponse().getOutputStream());
		}catch(Exception e){
			logger("OnlinepayController exportOnlinepayList:"+e.getMessage(),e);
			toRedirect(500);
		}
		return success();
	}
	
	
	/**
	 * 
	 * <p>Title: cancelAppOrder</p>
	 * <p> 功能描述:取消APP订单 </p>
	 * @param param
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/onlinepayManage/cancelAppOrder.json")
	public Object cancelAppOrder(String param) {
		boolean bln=checkRight();
		if(!bln) {
			return fail(Constant.Authority.CHECK_FALSE);
		}
		if(param==null) {
			return fail(Constant.EMPTY);
		}
		try {
			OnlinepayForm onlinepay=new Gson().fromJson(param,OnlinepayForm.class);
			if(StringUtils.isEmpty(onlinepay.getOrderno())) {
				return fail(Constant.ORDER_NOT_NULL);
			}
			int isAdd=onlinepayService.cancelOrder(onlinepay);
			if (isAdd == 1) {
				return success();
			}else {
				if (isAdd == -2) {
					return fail("该订单已发货，不能取消");
				}else if (isAdd == -3) {
					return fail("该订单已取消或不存在");
				}
				return fail(Constant.OPERATE_FAIL);
			}
		}catch (Exception e) {
			logger("OnlinepayController cancelAppOrder:"+e.getMessage(),e);
			return fail(Constant.EXCEPTION.OPERATE_FAIL);
		}
	}
	
	/**
	 * <p>Title: queryDevice</p>
	 * <p> 功能描述:app购买订单退款</p>
	 * @param param
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/onlinepayManage/refund.json")
	public Object refund(String param) {
		try {
			OnlinepayForm onlinepay = null;
			if (StringUtil.isBlank(param)) {
				onlinepay = new OnlinepayForm();
			}else {
				onlinepay = new Gson().fromJson(param, OnlinepayForm.class);
			}
			String res = onlinepayService.refund(onlinepay);
			if (Constant.SUCCESS.equals(res)) {
				return success();
			}else {
				return fail(res);
			}
		} catch (Exception e) {
			if (e instanceof OpServiceException) {
				return fail(e.getMessage());
			}
			e.printStackTrace();
			logger("DiscountManage refund:"+e.getMessage(),e);
			return fail(Constant.EXCEPTION.OPERATE_FAIL);
		}
	}
	
	@ResponseBody
	@RequestMapping("/noCheck_queryFactorys.json")
	public Object queryFactorys(){
		List<Factory> factorys = onlinepayService.queryFactorys();
		return success(factorys);
	}
	
	/**
	 * <p>Title: queryDevice</p>
	 * <p> 功能描述:获取促销订单 </p>
	 * @param param
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/onlinepayManage/queryDiscountList.json")
	public Object queryDevice(String param) {
		boolean bln = checkRight();
		if (!bln) {
			return fail(Constant.Authority.CHECK_FALSE);
		}
		try {
			DiscountOrderVO discountOrder = null;
			if (StringUtil.isBlank(param)) {
				discountOrder = new DiscountOrderVO();
			}else {
				discountOrder = new Gson().fromJson(param, DiscountOrderVO.class);
			}
			return success(onlinepayService.getDiscountList(discountOrder));
		} catch (Exception e) {
			logger("DiscountManage queryDiscountList:"+e.getMessage(),e);
			return fail(Constant.EXCEPTION.OPERATE_FAIL);
		}
	}
	/**
	 * <p>Title: sendDiscountDevice</p>
	 * <p> 功能描述:配送促销订单 </p>
	 * @param param
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/onlinepayManage/sendDiscountDevice.json")
	public Object sendDiscountDevice(String param) {
		boolean bln = checkRight();
		if (!bln) {
			return fail(Constant.Authority.CHECK_FALSE);
		}
		if (StringUtil.isBlank(param)) {
			return fail(Constant.EMPTY);
		}
		try {
			DiscountOrderVO discountOrder =  new Gson().fromJson(param, DiscountOrderVO.class);
			if (discountOrder == null || StringUtil.isBlank(discountOrder.getSendtime())) {
				return fail(Constant.EMPTY);
			}
			int isAdd = onlinepayService.sendDiscountDevice(discountOrder, getUserId());
			if (isAdd == 1) {
				return success();
			}else {
				if (isAdd == -1) {
					return fail(Constant.DiscountOrder.ORDER_NOT_EXIST_AT_SHCOOL);
				}else if (isAdd == -2) {
					return fail("该订单已发货，不能重复发货");
				}
				return fail(Constant.OPERATE_FAIL);
			}
		} catch (Exception e) {
			logger("DiscountManage sendDiscountDevice:"+e.getMessage(),e);
			return fail(Constant.EXCEPTION.OPERATE_FAIL);
		}
	}
	
	/**
	 * <p>Title: sendDiscountDevice</p>
	 * <p> 功能描述:取消促销订单 </p>
	 * @param param
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/onlinepayManage/cancelDiscount.json")
	public Object cancelDiscountOrder(String param) {
		boolean bln = checkRight();
		if (!bln) {
			return fail(Constant.Authority.CHECK_FALSE);
		}
		if (StringUtil.isBlank(param)) {
			return fail(Constant.EMPTY);
		}
		try {
			OnlinepayForm discountOrder =  new Gson().fromJson(param, OnlinepayForm.class);
			if (discountOrder == null || StringUtil.isBlank(discountOrder.getOrderno())) {
				return fail(Constant.EMPTY);
			}
			int isAdd = onlinepayService.cancelOrder(discountOrder);
			if (isAdd == 1) {
				return success();
			}else {
				if (isAdd == -2) {
					return fail("该订单已发货，不能取消");
				}else if (isAdd == -3) {
					return fail("该订单已取消或不存在");
				}
				return fail(Constant.OPERATE_FAIL);
			}
		} catch (Exception e) {
			logger("DiscountManage cancelDiscountOrder:"+e.getMessage(),e);
			return fail(Constant.EXCEPTION.OPERATE_FAIL);
		}
	}
	
	/**
	 * <p>Title: exportDiscountList</p>
	 * <p> 功能描述:导出促销订单Excel </p>
	 * @param param
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/onlinepayManage/export_discountList.json")
	public Object exportDiscountList(String param) {
		boolean bln = checkRight();
		if (!bln) {
			toRedirect(404);
		}
		try {
			DiscountOrderVO discountOrder = null;
			if (StringUtil.isBlank(param)) {
				discountOrder = new DiscountOrderVO();
			}else {
				discountOrder = new Gson().fromJson(param, DiscountOrderVO.class);
			}	
			ArrayList<String> listParm = new ArrayList<String>();
			listParm.add(Constant.Device.DEVICE_TYPE);
			listParm.add(Constant.Excel.PAY_STATUS);
			listParm.add(Constant.Excel.PAY_MONEY);
			listParm.add(Constant.Excel.DEVICE_PACKAGE);
			listParm.add(Constant.Excel.PAY_TIME);
			listParm.add(Constant.Excel.OBJECT_NAME);
			listParm.add(Constant.Excel.GRADE);
			listParm.add(Constant.Excel.SCHOOLNAME);
			listParm.add(Constant.Excel.PEOPLE_TEL);
			//listParm.add(Constant.Excel.IMEI);
			listParm.add(Constant.Excel.DISCOUNT_SEND_TIME);
			listParm.add(Constant.Excel.STATUS);
			List<List<String>> listValue = new LinkedList<List<String>>();
			if (discountOrder.getPagecount() == null) {
				discountOrder.setPagecount(Integer.parseInt(Constant.Excel.EXPORT_ROWS));
			}
			PageModel<DiscountOrderVO> page = onlinepayService.getDiscountList(discountOrder);
			List<DiscountOrderVO> list = page.getQueryresult();
			for (DiscountOrderVO discountOrderVO : list) {
				List<String> value = new LinkedList<String>();
				value.add(discountOrderVO.getDevicetype());
				value.add(discountOrderVO.getPaystatus());
				value.add(discountOrderVO.getPaymoney() == null ? null : discountOrderVO.getPaymoney().toString());
				if (discountOrderVO.getDevicepackage() != null) {
					value.add(new StringBuilder().append(discountOrderVO.getDevicepackage()).append("年").toString());
				}else {
					value.add(null);
				}
				value.add(discountOrderVO.getCreatetime());
				value.add(discountOrderVO.getStudentname());
				value.add(discountOrderVO.getClassname());
				value.add(discountOrderVO.getSchoolname());
				value.add(discountOrderVO.getTelephone());
				//value.add(discountOrderVO.getMdtid());
				value.add(discountOrderVO.getSendtime());
				if (discountOrderVO.getStatus() != null && discountOrderVO.getStatus() == 0) {
					value.add("已取消");
				}else {
					if (discountOrderVO.getSendtime() == null) {
						value.add("未配送");
					}else {
						value.add("已配送");
					}
				}
				listValue.add(value);
			}
			// 创建新的Excel 工作簿
			HSSFWorkbook workbook = exportDeviceList(Constant.Excel.DISCOUNT_EXCEL, listParm, listValue);
			HttpServletResponse response = this.getResponse();
			response.setHeader("Content-disposition", "attachment;filename=discountlist.xls");
			workbook.write(this.getResponse().getOutputStream());
		} catch (Exception e) {
			logger("DiscountManage export_discountList:"+e.getMessage(),e);
			toRedirect(500);
		}
		return success();
	}
	
	/**
	 * <p>Title: exportDiscountList</p>
	 * <p> 功能描述:获取学校未配送订单数 </p>
	 * @param param
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/onlinepayManage/getordercounts.json")
	public Object getordercounts(String param) {
		boolean bln = checkRight();
		if (!bln) {
			return fail(Constant.Authority.CHECK_FALSE);
		}
		try {
			DiscountOrderVO discountOrder = null;
			if (StringUtil.isBlank(param)) {
				return fail(Constant.DiscountOrder.SCHOOLNAME_NOT_NULL);
			}else {
				discountOrder = new Gson().fromJson(param, DiscountOrderVO.class);
			}
			if (discountOrder == null || com.suixun.util.StringUtil.isEmpty(discountOrder.getSchoolname())) {
				return fail(Constant.DiscountOrder.SCHOOLNAME_NOT_NULL);
			}
			int count = onlinepayService.getOrderCount(discountOrder.getSchoolname(), 3);
			Map<String,Object> result = new HashMap<>();
			result.put("count", count);
			return success(result);
		} catch (Exception e) {
			logger("DiscountManage getordercounts:"+e.getMessage(),e);
			toRedirect(500);
		}
		return success();
	}
	
	/**
	 * <p>Title: exportDiscountList</p>
	 * <p> 功能描述:获取未配送订单的学校名称 </p>
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/onlinepayManage/getSchoolsByDiscount.json")
	public Object getSchoolsByDiscount() {
		boolean bln = checkRight();
		if (!bln) {
			return fail(Constant.Authority.CHECK_FALSE);
		}
		try {
			return success(onlinepayService.getSchoolNames(3));
		} catch (Exception e) {
			logger("onlinepayManage getSchoolsByDiscount:"+e.getMessage(),e);
			toRedirect(500);
		}
		return success();
	}
	/**
	 * 
	 * <p>Title: getChongqingOrders</p>
	 * <p> 功能描述:获取重庆订单列表 </p>
	 * @param param
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/onlinepayManage/getChongqingOrders.json")
	public Object getChongqingOrders(String param) {
		
		try {
			ChongqingOrderVO order = checkParam(param, ChongqingOrderVO.class);
			return success(onlinepayService.
					listChongqingOrder(order.getTelephone(), order.getSendstatus(),
							order.getStatus(), order.getPagenumber(), order.getPagecount()));
		} catch (Exception e) {
			e.printStackTrace();
			logger("onlinepayManage getChongqingOrders:"+e.getMessage(),e);
			toRedirect(500);
		}
		return success();
	}
	
	/**
	 * 
	 * <p>Title: getNotSendCounts</p>
	 * <p> 功能描述:获取未发货数量 </p>
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/onlinepayManage/getNotSendCounts.json")
	public Object getNotSendCounts() {
		try {
			return success(onlinepayService.getNotSendCounts());
		} catch (Exception e) {
			e.printStackTrace();
			logger("onlinepayManage getNotSendCounts:"+e.getMessage(),e);
			toRedirect(500);
		}
		return success();
	}
	
	/**
	 * <p>Title: getIdCard</p>
	 * <p> 功能描述:获取身份证图片 </p>
	 * @param telephone
	 */
	@ResponseBody
	@RequestMapping(value = "/onlinepayManage/getIdCard.json")
	public Object getIdCard(String telephone, String cardporperty) {
		try {
			if(StringUtil.isBlank(telephone) || StringUtil.isBlank(cardporperty)){
				return null;
			}
			return success(onlinepayService.getIdCardByPeopleTel(telephone,cardporperty));
		} catch (Exception e) {
			logger("onlinepayManage getIdCard:"+e.getMessage(),e);
			toRedirect(500);
		} 
		
		return null;
	}
	
	@ResponseBody
	@RequestMapping("/onlinepayManage/export_chongqingOrders.json")
	public Object exportChongqingOrders(String param) {

		try{
			ChongqingOrderVO order = checkParam(param, ChongqingOrderVO.class);
			ArrayList<String> listParm = new ArrayList<String>();
			listParm.add(Constant.Device.DEVICE_TYPE);
			listParm.add(Constant.Excel.PAY_STATUS);
			listParm.add(Constant.Excel.ORDERNO);
			listParm.add(Constant.Excel.PAY_MONEY);
			listParm.add(Constant.Excel.PAY_TIME);
			listParm.add(Constant.Excel.OBJECT_NAME);
			listParm.add(Constant.Excel.GRADE);
			listParm.add(Constant.Excel.SCHOOLNAME);
			listParm.add(Constant.Excel.PEOPLE_TEL);
			listParm.add(Constant.Excel.IMEI);
			listParm.add(Constant.Excel.DISCOUNT_SEND_TIME);
			listParm.add(Constant.Excel.STATUS);
			List<List<String>> listValue=new LinkedList<List<String>>();
			//限制最多导出1万条数据，前端可以通过细化查询条件来多次导出
			if(order.getPagecount() == null || order.getPagecount() == 15) {
				order.setPagecount(Integer.parseInt(Constant.Excel.EXPORT_ROWS));
			}
			PageModel<ChongqingOrderVO> pageModel = 
					onlinepayService.listChongqingOrder(order.getTelephone(), order.getSendstatus(), order.getStatus(), 0, order.getPagecount());
			List<ChongqingOrderVO> pageList = pageModel.getQueryresult();
			//限制最多导出6万条数据，前端可以通过细化查询条件来多次导出
			if (pageList != null && pageList.size() > 0) {
				for(ChongqingOrderVO orderInfo : pageList) {
					List<String> value = new LinkedList<String>();
					if (orderInfo.getDevicetype() != null) {
						if (orderInfo.getDevicetype() == 1) {
							value.add("手表");
						}else if (orderInfo.getDevicetype() == 2) {
							value.add("智能学生证");
						}else if (orderInfo.getDevicetype() == 3) {
							value.add("考勤卡");
						}else {
							value.add("");
						}
					}else {
						value.add("");
					}
					value.add(orderInfo.getPaystatus());
					value.add(orderInfo.getOrderno());
					value.add(orderInfo.getPaymoney() == null ? null : orderInfo.getPaymoney().toString());
					value.add(orderInfo.getCreatetime());
					value.add(orderInfo.getStudentname());
					value.add(orderInfo.getClassname());
					value.add(orderInfo.getSchoolname());
					value.add(orderInfo.getTelephone());
					value.add(orderInfo.getMdtid());
					value.add(orderInfo.getSendtime());
					if (orderInfo.getStatus() != null && orderInfo.getStatus() == 0) {
						value.add("已取消");
					}else {
						if (StringUtil.isBlank(orderInfo.getSendtime())) {
							value.add("未配送");
						}else {
							value.add("已配送");
						}
					}
					listValue.add(value);
				}
			}
			
			// 创建新的Excel 工作簿
			HSSFWorkbook workbook = exportDeviceList("重庆订单",listParm,listValue);
			HttpServletResponse response=this.getResponse();
			response.setHeader("Content-disposition", "attachment;filename=chongqingOrders.xls");
			workbook.write(this.getResponse().getOutputStream());
		}catch(Exception e){
			e.printStackTrace();
			logger("OnlinepayController exportChongqingOrders:"+e.getMessage(),e);
			toRedirect(500);
		}
		return success();
	}
	/**
	 * <p>Title: getRenewalOrders</p>
	 * <p> 功能描述:获取续费订单 </p>
	 * @param param
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/onlinepayManage/getRenewalOrders.json")
	public Object getRenewalOrders(String param) {
		try {
			RenewalOrderVO order = checkParam(param, RenewalOrderVO.class);
			return success(onlinepayService.
				listRenewalOrders(order.getRenewalaccount(), order.getMdtid(), order.getPagenumber(), order.getPagecount(), order.getOrderType()));
		} catch (Exception e) {
			e.printStackTrace();
			logger("onlinepayManage getRenewalOrders:"+e.getMessage(),e);
			toRedirect(500);
		}
		return success();
	}
	/**
	 * <p>Title: exportRenewalOrders</p>
	 * <p> 功能描述:导出app缴费订单 </p>
	 * @param param
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/onlinepayManage/export_renewalOrders.json")
	public Object exportRenewalOrders(String param) {

		try{
			RenewalOrderVO order = checkParam(param, RenewalOrderVO.class);
			ArrayList<String> listParm = new ArrayList<String>();
			listParm.add("订单号");
			listParm.add("设备IMEI号");
			listParm.add("缴费账号");
			listParm.add("缴费金额");
			listParm.add("缴费年限");
			listParm.add("缴费时间");
			listParm.add("缴费状态");
			List<List<String>> listValue=new ArrayList<List<String>>();
			//限制最多导出1万条数据，前端可以通过细化查询条件来多次导出
			if(order.getPagecount() == 15) {
				order.setPagecount(Integer.parseInt(Constant.Excel.EXPORT_ROWS));
			}
			PageModel<RenewalOrderVO> pageModel = 
					onlinepayService.listRenewalOrders(order.getRenewalaccount(), order.getMdtid(), order.getPagenumber(), order.getPagecount(), order.getOrderType());
			List<RenewalOrderVO> pageList = pageModel.getQueryresult();
			//限制最多导出6万条数据，前端可以通过细化查询条件来多次导出
			String statusStr = "已缴费";
			if (pageList != null && pageList.size() > 0) {
				for(RenewalOrderVO orderInfo : pageList) {
					List<String> value = new LinkedList<String>();
					value.add(orderInfo.getOrderno());
					value.add(orderInfo.getMdtid());
					value.add(orderInfo.getRenewalaccount());
					value.add(orderInfo.getRenewalmoney() == null ? null : orderInfo.getRenewalmoney().toString());
					value.add(orderInfo.getRenewalPackage());
					value.add(orderInfo.getRenewaltime());
					if (orderInfo.getStatus() != null) {
						switch(orderInfo.getStatus()) {
							case 2:
								statusStr = "退款中";
								break;
							case 3:
								statusStr = "退款成功";
								break;
							case -1:
								statusStr = "退款失败";
								break;
						}
					}else {
						statusStr = "未知";
					}
					value.add(statusStr);
					listValue.add(value);
				}
			}
			
			// 创建新的Excel 工作簿
			HSSFWorkbook workbook = exportDeviceList("APP缴费订单",listParm,listValue);
			HttpServletResponse response=this.getResponse();
			response.setHeader("Content-disposition", "attachment;filename=renewalOrders.xls");
			workbook.write(this.getResponse().getOutputStream());
		}catch(Exception e){
			e.printStackTrace();
			logger("OnlinepayController exportRenewalOrders:"+e.getMessage(),e);
			toRedirect(500);
		}
		return success();
	}
	
	
	/**
	 * <p>Title: queryDevice</p>
	 * <p> 功能描述:获取新促销订单 </p>
	 * @param param
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/onlinepayManage/queryNewDiscountList.json")
	public Object queryNewDiscountList(String param) {
		try {
			DiscountOrderVO discountOrder = null;
			if (StringUtil.isBlank(param)) {
				discountOrder = new DiscountOrderVO();
			}else {
				discountOrder = new Gson().fromJson(param, DiscountOrderVO.class);
			}
			return success(onlinepayService.getNewDiscountPage(discountOrder));
		} catch (Exception e) {
			logger("DiscountManage queryDiscountList:"+e.getMessage(),e);
			return fail(Constant.EXCEPTION.OPERATE_FAIL);
		}
	}
	
	@ResponseBody
	@RequestMapping(value = "/onlinepayManage/sendNewDiscountDevice.json")
	public Object sendNewDiscountDevice(String param) {
		if (StringUtil.isBlank(param)) {
			return fail(Constant.EMPTY);
		}
		try {
			DiscountOrderVO discountOrder =  new Gson().fromJson(param, DiscountOrderVO.class);
			if (discountOrder == null || StringUtil.isBlank(discountOrder.getSendtime())) {
				return fail(Constant.EMPTY);
			}
			int isAdd = onlinepayService.sendNewDiscountDevice(discountOrder, getUserId());
			if (isAdd == 1) {
				return success();
			}else {
				if (isAdd == -1) {
					return fail(Constant.DiscountOrder.ORDER_NOT_EXIST_AT_SHCOOL);
				}else if (isAdd == -2) {
					return fail("该订单已发货，不能重复发货");
				}
				return fail(Constant.OPERATE_FAIL);
			}
		} catch (Exception e) {
			logger("DiscountManage sendNewDiscountDevice:"+e.getMessage(),e);
			return fail(Constant.EXCEPTION.OPERATE_FAIL);
		}
	}
	
	@ResponseBody
	@RequestMapping(value = "/onlinepayManage/cancelNewDiscount.json")
	public Object cancelNewDiscount(String param) {
		boolean bln = checkRight();
		if (!bln) {
			return fail(Constant.Authority.CHECK_FALSE);
		}
		if (StringUtil.isBlank(param)) {
			return fail(Constant.EMPTY);
		}
		try {
			OnlinepayForm discountOrder =  new Gson().fromJson(param, OnlinepayForm.class);
			if (discountOrder == null || StringUtil.isBlank(discountOrder.getOrderno())) {
				return fail(Constant.EMPTY);
			}
			int isAdd = onlinepayService.cancelOrder(discountOrder);
			if (isAdd == 1) {
				return success();
			}else {
				if (isAdd == -2) {
					return fail("该订单已发货，不能取消");
				}else if (isAdd == -3) {
					return fail("该订单已取消或不存在");
				}
				return fail(Constant.OPERATE_FAIL);
			}
		} catch (Exception e) {
			logger("DiscountManage cancelNewDiscount:"+e.getMessage(),e);
			return fail(Constant.EXCEPTION.OPERATE_FAIL);
		}
	}
	
	
	@ResponseBody
	@RequestMapping(value = "/onlinepayManage/export_NewDiscountList.json")
	public Object exportNewDiscountList(String param) {
		try {
			DiscountOrderVO discountOrder = null;
			if (StringUtil.isBlank(param)) {
				discountOrder = new DiscountOrderVO();
			}else {
				discountOrder = new Gson().fromJson(param, DiscountOrderVO.class);
			}	
			ArrayList<String> listParm = new ArrayList<String>();
			listParm.add(Constant.Device.DEVICE_TYPE);
			listParm.add(Constant.Excel.PAY_STATUS);
			listParm.add(Constant.Excel.PAY_MONEY);
			listParm.add(Constant.Excel.DEVICE_PACKAGE);
			listParm.add(Constant.Excel.PAY_TIME);
			listParm.add(Constant.Excel.OBJECT_NAME);
			listParm.add(Constant.Excel.GRADE);
			listParm.add(Constant.Excel.SCHOOLNAME);
			listParm.add(Constant.Excel.PEOPLE_TEL);
			listParm.add("收货地址");
			//listParm.add(Constant.Excel.IMEI);
			listParm.add(Constant.Excel.DISCOUNT_SEND_TIME);
			listParm.add(Constant.Excel.STATUS);
			List<List<String>> listValue = new LinkedList<List<String>>();
			if (discountOrder.getPagecount() == null) {
				discountOrder.setPagecount(Integer.parseInt(Constant.Excel.EXPORT_ROWS));
			}
			PageModel<DiscountOrderVO> page = onlinepayService.getNewDiscountPage(discountOrder);
			List<DiscountOrderVO> list = page.getQueryresult();
			for (DiscountOrderVO discountOrderVO : list) {
				List<String> value = new LinkedList<String>();
				value.add(discountOrderVO.getDevicetype());
				value.add(discountOrderVO.getPaystatus());
				value.add(discountOrderVO.getPaymoney() == null ? null : discountOrderVO.getPaymoney().toString());
				if (discountOrderVO.getDevicepackage() != null) {
					value.add(new StringBuilder().append(discountOrderVO.getDevicepackage()).append("年").toString());
				}else {
					value.add(null);
				}
				value.add(discountOrderVO.getCreatetime());
				value.add(discountOrderVO.getStudentname());
				value.add(discountOrderVO.getClassname());
				value.add(discountOrderVO.getSchoolname());
				value.add(discountOrderVO.getTelephone());
				//value.add(discountOrderVO.getMdtid());
				value.add(discountOrderVO.getAddress());
				value.add(discountOrderVO.getSendtime());
				if (discountOrderVO.getStatus() != null && discountOrderVO.getStatus() == 0) {
					value.add("已取消");
				}else {
					if (discountOrderVO.getSendtime() == null) {
						value.add("未配送");
					}else {
						value.add("已配送");
					}
				}
				listValue.add(value);
			}
			// 创建新的Excel 工作簿
			HSSFWorkbook workbook = exportDeviceList(Constant.Excel.DISCOUNT_EXCEL, listParm, listValue);
			HttpServletResponse response = this.getResponse();
			response.setHeader("Content-disposition", "attachment;filename=newdiscountlist.xls");
			workbook.write(this.getResponse().getOutputStream());
		} catch (Exception e) {
			logger("DiscountManage export_discountList:"+e.getMessage(),e);
			toRedirect(500);
		}
		return success();
	}
	
	@ResponseBody
	@RequestMapping(value = "/onlinepayManage/getnewordercounts.json")
	public Object getnewordercounts(String param) {
		boolean bln = checkRight();
		if (!bln) {
			return fail(Constant.Authority.CHECK_FALSE);
		}
		try {
			DiscountOrderVO discountOrder = null;
			if (StringUtil.isBlank(param)) {
				return fail(Constant.DiscountOrder.SCHOOLNAME_NOT_NULL);
			}else {
				discountOrder = new Gson().fromJson(param, DiscountOrderVO.class);
			}
			if (discountOrder == null || com.suixun.util.StringUtil.isEmpty(discountOrder.getSchoolname())) {
				return fail(Constant.DiscountOrder.SCHOOLNAME_NOT_NULL);
			}
			int count = onlinepayService.getOrderCount(discountOrder.getSchoolname(), 5);
			Map<String,Object> result = new HashMap<>();
			result.put("count", count);
			return success(result);
		} catch (Exception e) {
			logger("DiscountManage getordercounts:"+e.getMessage(),e);
			toRedirect(500);
		}
		return success();
	}
	
	@ResponseBody
	@RequestMapping(value = "/onlinepayManage/getSchoolsByNewDiscount.json")
	public Object getSchoolsByNewDiscount() {
		try {
			return success(onlinepayService.getSchoolNames(5));
		} catch (Exception e) {
			logger("onlinepayManage getSchoolsByNewDiscount:"+e.getMessage(),e);
			toRedirect(500);
		}
		return success();
	}
}
